import Cookies from "js-cookie"

export default {
  state: {
    isCollapse: false,
    currenMenu: null,
    menu: [],
    tabList: [
      {
        path: '',
        name: 'home',
        icon: 'home',
        lable: '首页'
      },
    ]
  },
  mutations: {
    collapsemenu(state) {
      state.isCollapse = !state.isCollapse

    },
    selectMenu(state, val) {
      if (val.name !== 'home') {
        state.currenMenu = val
        // const result= state.tabList.findIndext(item => item.name=== val.name)
        const result = state.tabList.find(item => item.name === val.name)

        if (result === undefined) {

          state.tabList.push(val)
          // console.log(state.tabList);
        }
      } else {
        state.currenMenu = null
      }
    },
    colseTag(state, val) {
      const result = state.tabList.findIndex(item => item.name === val.name)
      state.tabList.splice(result, 1)
    },
    //动态设置menu
    setmenu(state, val) {
      state.menu = val
      Cookies.set('menu', JSON.stringify(val))

    },
    clearMenmu(state) {
      state.menu = []
      Cookies.remove('menu')
    },
    addMenu(state, router) {

      if (!Cookies.get('menu')) {
        return
      }
      var  menuu = JSON.parse(Cookies.get('menu'))
      state.menu = menuu
      const menuArray = []
      menuu=Array.from(menuu)
      menuu.forEach(item => {
        if (item.children) {
          item.children = item.children.map(item => {
            item.component = () => import(`../src/view/${item.url}`)

            return item
          })
          menuArray.push(...item.children)
        } else {
          item.component = () => import(`../src/view/${item.url}`)
          menuArray.push(item)
        }

      });
      //路由的添加
      menuArray.forEach(item => {
        router.addRoute('Main', item);
      });
    }


  }
}